<?

//FUNCTION DECLARATIONS
function getReturnDate($VisitID)
{
	$queryReturnDates = "SELECT return_date
						FROM er_visit
						WHERE visit_id = '$VisitID'
						ORDER BY return_date";
	$resultReturnDates = r_db($queryReturnDates);
	echo "<td valign=\"top\">";
		while($returnDates = mysql_fetch_array($resultReturnDates))
		{
			echo "$returnDates[0] <br />";
		}
	echo "</td>";

}

function getNoOfVisits1($ClientID)
{
	$queryNoOfVisits = "SELECT er_Client.client_id, COUNT(er_visit.visit_id)
						FROM er_Client, er_visit
						WHERE er_Client.client_id=er_visit.client_id
						AND er_visit.visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
						AND er_Client.client_id='$ClientID'
						GROUP BY er_Client.client_id";
	$resultNoOfVisits = r_db($queryNoOfVisits);
	$rowNoOfVisits = mysql_fetch_array($resultNoOfVisits);
	$noOfVisits = $rowNoOfVisits[1];
	return $noOfVisits;
}
function getPaymentDetails1($ClientID, $rowPayment, $visitPaymentID)
{
	$queryVisit = "SELECT a.payment_amount
				FROM er_Payment_On_Visit as a INNER JOIN er_Visit as b ON b.visit_id = a.visit_id
				WHERE b.visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
					AND a.payment_id=$rowPayment
					AND b.visit_id = $visitPaymentID
					ORDER BY b.visit_date, b.visit_id";
	$resultVisit = r_db($queryVisit);
	return $resultVisit;
}
function getTotalAmountOnVisit1($ClientID, $VisitID)
{
	$queryErGiven = "SELECT SUM(er_Payment_On_Visit.payment_amount) as pa, er_visit.visit_id
						FROM er_visit, er_Payment_On_Visit
						WHERE er_visit.visit_id=er_Payment_On_Visit.visit_id
						AND er_visit.visit_id = $VisitID
						AND er_visit.visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
						GROUP BY er_visit.visit_date, er_visit.visit_id";
	$resultErGiven = r_db($queryErGiven);
	return $resultErGiven;
}

function getIncomeSources($VisitID)
{
	$query = "select er_income_source.income_type,
	er_client_income.income_amount,
	er_client_income.when_due,
	er_visit.visit_id
	FROM er_income_source,er_client_income,er_visit
	where er_client_income.income_id=er_income_source.income_id
	AND er_visit.visit_id=er_client_income.visit_id
	AND er_client_income.visit_id=$VisitID
	group by income_type";

	$results=r_db($query);
	echo "<td valign=\"top\">";

	while ($rowIncomeSource = mysql_fetch_array($results)) {

		$duedate =  $rowIncomeSource[2];
		$DbDOB=explode("-",$duedate);
		$yearDOB=$DbDOB[0];
		$monthDOB=$DbDOB[1];
		$dayDOB=$DbDOB[2];
		$duedate=$dayDOB."/".$monthDOB."/".$yearDOB;

		if($rowIncomeSource[0]<>"Select Client Income")
		{
			echo Bullet()."$rowIncomeSource[0] - $".number_format($rowIncomeSource[1],2);
			if ($duedate != '00/00/0000') {
				echo " - $duedate";
			}
			echo "<br />";
		}
	}
	echo "</td>";
}

function getEmergencyReliefReasons($VisitID)
{
	$query ="SELECT er_Emergency_Relief.er_reason, er_Relief_On_Visit.visit_id
			FROM er_Emergency_Relief,er_Relief_On_Visit
			WHERE	er_Relief_On_Visit.relief_id=er_Emergency_Relief.er_id
			AND er_Relief_On_Visit.visit_id = $VisitID
			ORDER BY er_Relief_On_Visit.visit_id";
	$results=r_db($query);
	echo "<td valign=\"top\">";
	while($rowErGiven = mysql_fetch_array($results))
	{
		echo Bullet()."$rowErGiven[0]<br>";
	}
	echo "</td>";
}



function getActionTakenReasons($VisitID)
{
	$query ="SELECT er_action_taken.action_taken, er_action_on_visit.visit_id
			FROM er_action_taken,er_action_on_visit
			WHERE	er_action_on_visit.action_id=er_action_taken.action_id
			AND er_action_on_visit.visit_id = $VisitID
			ORDER BY er_action_on_visit.visit_id";
	$results=r_db($query);
	echo "<td valign=\"top\">";
	while($rowErGiven = mysql_fetch_array($results))
	{
		echo Bullet()."$rowErGiven[0]<br>";
	}
	echo "</td>";
}




function Bullet () {
	echo "<span style=\"font-size: 10px; font-weight: bold; font-family: symbol;\">�</span> ";
}

function GetVisitDetails1($ClientID)
{
	global $sqlForYear;
	$queryVisitDate = "SELECT er_Client.client_id,
							  er_visit.visit_id,
							  er_visit.visit_date,
							  er_site.site_name,
							  er_visit.interviewer,
		  					  er_visit.action_taken,
							  er_visit.day_book_colour,
							  er_visit.day_book_page_no,
							  er_visit.no_children_assistted,
							  er_visit.no_adults_assistted,
							  er_visit.address,
							  er_housing_type.housing_type,
							  er_visit.phone_no,
							  er_visit.mobile_no,
							  er_income_status.income_status_type,
							  er_visit.other_housing_type,
							  er_visit.centrelink_loan_amt_borrowed,
							  er_visit.rent_paid_pf,
							  er_agency.agency_name,
							  er_visit.other_income_status,
							  er_visit.visit_recorded_date,
							  er_visit.suburb,
							  er_visit.state,
							  er_visit.postcode,
							  er_visit.er_given,
							  er_visit.er_reason,
							  er_visit.other_er,
							  er_visit.client_signature,
							  er_visit.er_amount_receipt_no,
							  er_visit.reason_cheque_payment,
							  er_visit.cheque_payee,
							  er_visit.other_payment_type,
							  er_visit.extra_food_parcels,
							  er_visit.other_income_source,
							  er_visit.referred_internal,
							  er_visit.referred_external,
							  er_visit.other_financial
							  FROM er_Client, er_visit,er_housing_type,er_income_status,er_agency, er_site
							  WHERE er_Client.client_id=er_visit.client_id
							  AND er_visit.site_visited_id=er_site.site_id
							  AND er_visit.visit_date >= ".$sqlForYear."
							  AND er_Client.client_id=$ClientID
							  AND er_visit.housing_type_id=er_housing_type.housing_type_id
							  AND er_visit.income_status_id=er_income_status.income_status_id
							  AND er_visit.agency_id=er_agency.agency_id
							  ORDER BY er_visit.visit_date, er_visit.visit_id";
	$resultVisit = r_db($queryVisitDate);
	return $resultVisit;
}

function getIDtype($idtype)
{
	$query="select identification_type from er_Identification_Type where identification_no='".$idtype."'";
	$resultIDtype = r_db($query);
	$row=mysql_fetch_array($resultIDtype);
	return $row;
}

global $client_id;
if (isset($_POST['clientID'])) {
	$client_id = $HTTP_POST_VARS['clientID'];
	createSessionClientId($client_id);
	print_r($_POST);
}
elseif (isset($_GET['clientID'])) {
	$client_id = $HTTP_GET_VARS['clientID'];
	createSessionClientId($client_id);
}
elseif (!empty($client_id)) {
	$client_id = $client_id;
}
elseif (isset($_SESSION['session_clientId'])) {
	$client_id = $_SESSION['session_clientId'];
}
?>

                <div id="tabs10">
                <ul>
                                <!-- CSS Tabs -->
<? echo "<li><a href=\"?page=visit_details&client_id=$client_id\"><span>Page 1</span></a></li>"; ?>
<? echo "<li><a href=\"?page=view_page_2&client_id=$client_id\"><span>Page 2</span></a></li>"; ?>
                </ul>
                </div>




</script><SCRIPT LANGUAGE="JavaScript">
<!--
function OpenWindow(url, name, width, height)
{
	popupWin = window.open(url, '', 'width=' + width + ', height=' + height + ',scrollbars,resizable')
}
-->
</script>

<?

global $ClientID;
if (!isset($ClientID)) {
	$ClientID = $client_id;
	//$_GET['ClientID'];
}
if ($ClientID > NULL) {
	$result = GetVisitDetails1($ClientID);
	$i = 0;
	$visit = array();

	while ($temp = mysql_fetch_array($result)) {
		$visit[$i] = $temp;
		$i++;
	}

if (isset($ClientID))
{
	$query = "SELECT * FROM er_Client WHERE client_id ='$ClientID'";
	$sql = r_db($query);
	$row = mysql_fetch_array($sql);
	$idtyperesult=getIDtype($row[10]);
	echo "<table border=\"0\" width=\"100%\" id=\"visit\" cellpadding=\"2\" cellspacing=\"0\"> ";
	echo "<tr><th colspan=\"2\" align=\"left\">Client Details</th><th colspan=\"2\" align=\"right\"><a href=\"javascript:void(OpenWindow('pages/form_visit_details.php?ClientID=$ClientID','',710, 400));\">Print Client's Visit Details</a><br /><i>Clients with over 8 visits may not print correctly</i></th></tr>";
	echo  "<tr><td width=\"20%\">Surname:</td><td width=\"30%\">$row[2]</td><td width=\"20%\">Given Name:</td><td width=\"30%\">".$row[3]."</td></tr>";

	$DOB =  $row[4];
	$DbDOB=explode("-",$DOB);
	$dayDOB=$DbDOB[2];
	$monthDOB=$DbDOB[1];
	$yearDOB=$DbDOB[0];
	$DOB=$dayDOB."/".$monthDOB."/".$yearDOB;

	if ($row[6]=="f")
		$gender="Female";
	else
		$gender="Male";
	echo  "<tr>"
	.	"<td>Date of Birth:</td><td>" . $DOB."</td>";
	echo "<td>Gender:</td><td>$gender</td>";
	echo  "</tr>";

	echo "<tr>"
	.	"<td>ID Type:<td>".$idtyperesult[0]. "</td>"
	.     "<td>ID Reference No:</td><td>" . $row[1]."</td>";
	echo "</tr>";
	$otheridvalue = $row[8];
	if ($otheridvalue != null)
	{
		echo   "<tr><td>&nbsp;</td><td>&nbsp;</td><td>Other ID Type:</td><td>" . $row[8]."</td></tr>";
	}

	print "   <input type=\"hidden\" name=\"selectedClientID\" value=\"". $row[0] . "\" />";
	print "   <input type=\"hidden\" name=\"selectedClientSurname\" value=\"". $row[2] . "\" />";
	print "   <input type=\"hidden\" name=\"selectedClientFirstName\" value=\"". $row[3] . "\" />";


	echo "</table>";
}

$noOfVisits = getNoOfVisits1($ClientID);
if ($noOfVisits > 0) {
?>
	<Br />
	<table id="visit" width="100%" cellpadding="2" cellspacing="0">
		<tr><th>Current Year Visit(s)</th>
			<?
			foreach ($visit as $v => $value) {
				if ($v < 3) {
					echo "<th class=\"green\">Visit #". ($v+1) ."</th>";
				}
				elseif ($v < 4) {
					echo "<th class=\"orange\">Visit #". ($v+1) ."</th>";
				}
				else {
					echo "<th class=\"red\">Visit #". ($v+1) ."</th>";
				}
			}
			?>
		</tr>
		<tr><td valign="top">Date</td>
			<?
			foreach ($visit as $v => $value) {
				$DbDOB=explode("-",$value['visit_date']);
				@$dayDOB=$DbDOB[2];
				@$monthDOB=$DbDOB[1];
				$yearDOB=$DbDOB[0];
				$dobDate=$dayDOB."/".$monthDOB."/".$yearDOB;
				echo "<td>$dobDate</td>";
			}
			?>
		</tr>
		<tr><td>Site</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['site_name']."</td>";
			}
			?>
		</tr>
		<tr><td valign="top">Address</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['address']."</td>";
			}
			?>
		</tr>
		<tr><td valign="top">Suburb</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['suburb']."</td>";
			}
			?>
		</tr>
		<tr><td valign="top">State</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['state']."</td>";
			}
			?>
		</tr>
		<tr><td valign="top">Post Code
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['postcode']."</td>";
			}
			?>
		</tr>
		<tr><td valign="top">Phone Number</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['phone_no']."</td>";
			}
			?>
		</tr>
		<tr><td valign="top">Mobile Number</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['mobile_no']."</td>";
			}
			?>
		</tr>
		<tr><td valign="top">No. of Children assisted</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['no_children_assistted']."</td>";
			}
			?>
		</tr>
		<tr><td valign="top">No. of Adults assisted</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['no_adults_assistted']."</td>";
			}
			?>
		</tr>
		<tr>
			<th>Assistance Provided</th>
		</tr>

	<?
	//retrieve visit details

	//print visit number
	/*
	echo "<tr>";
	echo "<td  width=\"25%\">&nbsp;</td>";
	for($i=1;$i<=$noOfVisits;$i++)
	{
		echo "<td>Visit $i</td>";
	}
	echo "</tr>";

	*/
	//end print visit number

	$queryPayment = "SELECT * FROM er_payment";
	$resultPayment = r_db($queryPayment);
	for($i=1;$i<=$noOfVisits+1;$i++)
	{
		while ($rowPayment = mysql_fetch_array($resultPayment))
		{
			echo "<tr><td valign=\"top\" width=\"25%\">$rowPayment[1] ($)</td>";

			$sql = "SELECT visit_id from er_visit WHERE client_id = $ClientID AND visit_date >= ".$sqlForYear." ORDER BY visit_date, visit_id";
			$result = r_db($sql);
			$visits = NULL;

			while ($visits = mysql_fetch_array($result)) {
				$arrVisitDetails = getPaymentDetails1($ClientID,$rowPayment['payment_id'], $visits['visit_id']);
				$rowVisit = mysql_fetch_array($arrVisitDetails);
				if ($rowVisit['payment_amount'] > 0) {
					{

						//Print the amount in the appropriate visit coloumn

						$width = 75/($noOfVisits+1);
						round($width,0);
						echo "<td valign=\"top\" align=\"left\">". number_format($rowVisit['payment_amount'],2);
						echo "</td>";

					}
				}
				else {
					$width = 75/($noOfVisits+1);
					round($width,0);
					echo "<td valign=\"top\" align=\"left\">&nbsp;</td>";
				}
			}
			echo "</tr>\n";
		}
	}
	echo "<tr><td bgcolor=\"yellow\">Total Amount Given ($)</td>";

	$sql = "SELECT visit_id from er_visit WHERE client_id = $ClientID AND visit_date >= ".$sqlForYear." ORDER BY visit_date";
	$result = r_db($sql);
	$visits = NULL;

	while ($visits = mysql_fetch_array($result))
	{
		$resultPayment = getTotalAmountOnVisit1($ClientID, $visits['visit_id']);
		$rowErGiven = mysql_fetch_array($resultPayment);
		echo "<td align=\"left\" bgcolor=\"yellow\">";
		echo number_format($rowErGiven['pa'],2)  ;
		echo "&nbsp;</td>";
	}

	echo "</tr>\n";
	?>
		<tr><td valign="top">Other Payment Type: </td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['other_payment_type']."</td>";
			}
			?>
		</tr>

		<tr><td valign="top">Extra Food Parcels: </td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['extra_food_parcels']."</td>";
			}
			?>
		</tr>

			<tr><td valign="top">Cheque Number: </td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['er_amount_receipt_no']."</td>";
			}
			?>
		</tr>
			<tr><td valign="top">Cheque Payee: </td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['cheque_payee']."</td>";
			}
			?>
		</tr>
			<tr><td valign="top">Reason for cheque: </td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['reason_cheque_payment']."</td>";
			}
			?>
		</tr>

		<tr>
			<tr><td valign="top">Referred Internal? </td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['referred_external']."</td>";
			}
			?>
		</tr>

		<tr>
			<tr><td valign="top">Referred External? </td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['referred_internal']."</td>";
			}
			?>
		</tr>



		<tr><th>Sources of Household Income</th></tr>
		<tr><td valign="top">Sources of Household Income($)</td>
			<?
			foreach ($visit as $v => $value) {
				getIncomeSources($value['visit_id']);
			}
			?>
		</tr>
		<tr><td valign="top"> Other Income Source</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['other_income_source']."</td>";
			}
			?>
		</tr>

		<tr><th>Income Status</th></tr>
		<tr><td valign="top">Income Status</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['income_status_type']."</td>";
			}
			?>
		</tr>
		<tr><td valign="top"> Other Income Status</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['other_income_status']."</td>";
			}
			?>
		</tr>

		<tr><th>Housing Type</th></tr>
		<tr><td valign="top">Housing Type</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['housing_type']."</td>";
			}
			?>
		</tr>
		<tr><td valign="top">Other Housing Type</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['other_housing_type']."</td>";
			}
			?>
		</tr>

		<tr><td valign="top">Rent Paid per fortnight ($)</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>$".number_format($value['rent_paid_pf'],2)."</td>";
			}
			?>
		</tr>

		<tr><th>Reason for Emergency Relief</th></tr>
		<tr><td valign="top">Reasons for seeking assistance</td>
			<?
			foreach ($visit as $v => $value) {
				getEmergencyReliefReasons($value['visit_id']);
			}
			?>
		</tr>
		<tr>
		<td valign="top"> Other Reason for ER</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['other_er']."</td>";
			}
			?>
		</tr>

		<tr><td valign="top">Client Referred By</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['agency_name']."</td>";
			}
			?>
		</tr>

		<tr><th>Action Taken Associated with E.R.</th></tr>
		<tr><td valign="top">Action Taken</td>
			<?
			foreach ($visit as $v => $value) {
				getActionTakenReasons($value['visit_id']);
			}
			?>
		</tr>
		<tr><td valign="top">Return Date</td>
			<?
			foreach ($visit as $v => $value) {
				getReturnDate($value['visit_id']);
			}
			?>
		</tr>
		<tr>
		<td valign="top"> Other Action Taken (Financial Only)</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['action_taken']."</td>";
			}
			?>
		</tr>


		<tr><td valign="top">Interviewer</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['interviewer']."</td>";
			}
			?>
		</tr>
	</table>
<?
	}
	else {
		echo "<p>This client has no visits within the last year.</p>";
	}
}
//finish visit details



?>


